// pages/mine/mine.js
var that,
  api = new (require('../../utils/api.js')),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ismember: !1,
    isload: !1,
    isoldrel: !1,
    isupgrade: !1,
    swiperlist: [
      { src: 'https://czl.sskj.公司/src/jiayouzhan_index_swiper.png', id: 1 },
      { src: 'https://czl.sskj.公司/src/jiayouzhan_index_swiper.png', id: 1 },
      { src: 'https://czl.sskj.公司/src/jiayouzhan_index_swiper.png', id: 1 },
    ],
    showgift: !1,
    showygift: !1,
    isnew: !1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    console.log(options, 'optionsoptions');
    let shareKey = '', shareinfo = ''
    if (options.shareKey) {
      shareKey = options.shareKey
      shareinfo = JSON.parse(options.userinfo)
    }
    that.setData({ shareKey, shareinfo }, () => {
      if (app.logininfo) {
        that.getdata();
      } else {
        util.setwatcher(app, this.afterlogin)
      }
    })
  },
  afterlogin: {
    logininfo: res => {
      that.getdata();
      // that.getCoupon()
    }
  },
  getdata() {
    that.setData({ isload: !0 })
    // wx.navigateTo({
    //   url: '../userlogin/userlogin',
    // })
    api.获取超级会员信息(res=>{
      if(res.data.code==0){
        let superinfo = res.data.entity.data
        that.setData({isSuper:superinfo.isSuper==1})
       wx.setStorageSync('superinfo',superinfo )
      } 
     })
    console.log(123);
   

    console.log(456);

    // api.test_cleanvip(10471,res=>{ 

    // })


    api.getuserdetail(res => {
      wx.hideLoading({
        complete: (res) => { },
      })
      if (res.data.code == 0) {
        wx.setStorageSync('amount', res.data.entity.data.totalAmount)
        wx.setStorageSync('userInfo', res.data.entity.data)
        let data = res.data.entity.data
        let { tipsInfo } = res.data.entity.data
        console.log(res.data.entity.data,'tipsInfo');
        if(tipsInfo.type){
          that.setData({ismember:!0,tipsInfo})
        }
        if (data.oldRel != '' && data.oldRel != null) {
          that.setData({ isoldrel: !0 })
        }
        // if((data.litPic==''&&data.name=='')||(data.litPic=='null'&&data.name=='null')){
        //   // if(data.litPic&&data.name){
        //   wx.navigateTo({
        //     url: '../userlogin/userlogin',
        //   })
        // }
        let usertype = data.levelName == '普通用户' || data.levelName == '普通会员' ? 'common' : data.levelName == '入门会员' ? 'introduction' : data.levelName == '皓银会员' ? 'brightsilver' : 'gold'
        data.totalAmountarr = Number(data.totalAmount).toFixed(2).split('.')
        data.totalDiscountarr = Number(data.totalDiscount).toFixed(2).split('.')
        that.setData({ data, usertype }, () => {
          that.getshare()
        })
      }
      else if (res.data.code == 20611) {
        wx.setStorageSync('amount', res.data.entity.data.totalAmount)
        wx.setStorageSync('userInfo', res.data.entity.data)
        let data = res.data.entity.data
        data.litPic == ''
        data.name == ''
        let usertype = data.levelName == '普通用户' || data.levelName == '普通会员' ? 'common' : data.levelName == '入门会员' ? 'introduction' : data.levelName == '皓银会员' ? 'brightsilver' : 'gold'
        data.totalAmountarr = Number(data.totalAmount).toFixed(2).split('.')
        data.totalDiscountarr = Number(data.totalDiscount).toFixed(2).split('.')
        that.setData({ data, usertype }, () => {
          that.getshare()
        })
        // wx.navigateTo({
        //   url: '../userlogin/userlogin',
        // })
      }
    })
    let posId = 1
    api.getfocus(posId, res => {
      if (res.data.code == 0) {
        let { infoArr } = res.data.entity.data
        console.log(infoArr, 'infoArrinfoArrinfoArr');
        that.setData({ infoArr })
        console.log(res.data, 'hhgjhghjghgfgf');
      }
    })
    let { corpBulkTag,corpInvoiceTag,isLock } = app.logininfo
    that.setData({ corpBulkTag ,corpInvoiceTag,isLock})
  },
  getshare() {
    let { data, shareKey, shareinfo } = that.data
    if (shareKey != '') {
      if (data.userId != shareinfo.userId) {
        that.setData({ showgift: !0 })
      } else {
        wx.navigateTo({
          url: '../sendoutGift/sendoutGift',
        })
      }
    }
  },
  close() {
    that.setData({ showgift: !1, showygift: !1, isnew: !1,ismember:!1 })
  },
  receivebtn() {
    let { shareKey } = that.data
    console.log(shareKey, 'shareKeyshareKey');
    api.userGrabcoupon(shareKey, res => {
      console.log(res.data, 'userGrabcoupon');
      if (res.data.code == 0) {
        console.log(res.data, 'res.data');
        let { data } = res.data.entity
        console.log(data, 'datadata');
        that.setData({ showgift: !1 })
        wx.navigateTo({
          url: '../giftList/giftList?userinfo=' + JSON.stringify(data),
        })
      } else if (res.data.code == 21303) {
        that.setData({ showgift: !1, showygift: !0 })
      } else if (res.data.code == 21305) {
        that.setData({ isnew: !0, showgift: !1, showygift: !1 })
      }
    })
  },
  toshop() {
    wx.switchTab({
      url: '../shop/shop',
    })
  },
  tocoupons() {
    wx.navigateTo({
      url: '../coupons/coupons',
    })
  },
  // 授权微信登录
  getUserProfile(e) {
    let { data } = that.data
    wx.getUserProfile({
      desc: '用于完善用户资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        let userinfo = res.userInfo
        console.log(res.userInfo, 'resres');
        if (res.userInfo) {
          api.updatewxinfo({ nickName: userinfo.nickName, avatarUrl: userinfo.avatarUrl }, res => {
            console.log(res, 'resresresres');
            // let getuserInfo=wx.getStorageSync('userInfo')
            data.litPic = userinfo.avatarUrl
            data.name = userinfo.nickName
            console.log(data, 'dataaaaaaaa');
            that.setData({ data })
            wx.setStorageSync('userInfo', data)
            if (res.data.code == 0) {
              wx.showToast({
                title: '授权成功',
                icon: 'none',
                duration: 1000
              })
            }
          })
        } else {
          console.log("点击了拒绝授权");
        }
      },
      fail(res) {
        wx.showToast({
          title: '拒绝授权将无法进入小程序，请重新点击授权',
          icon: 'none'
        })
        console.log(res, 'ressss');
      },
    })
  },

  // bindGetUserInfo(e) {
  //   console.log(e.detail.userInfo)
  //   let userinfo = e.detail.userInfo, { data } = that.data
  //   if (e.detail.userInfo) {
  //     api.updatewxinfo({ nickName: userinfo.nickName, avatarUrl: userinfo.avatarUrl }, res => {
  //       console.log(res, 'resresresres');
  //       // let getuserInfo=wx.getStorageSync('userInfo')
  //       data.litPic = userinfo.avatarUrl
  //       data.name = userinfo.nickName
  //       that.setData({ data })
  //       wx.setStorageSync('userInfo', data)
  //       if (res.data.code == 0) {
  //         wx.showToast({
  //           title: '授权成功',
  //           icon: 'none',
  //           duration: 1000
  //         })
  //       }
  //     })
  //   } else {

  //   }
  // },
  // 授权手机号
  getPhoneNumber(e) {
    let { data } = that.data, { url } = e.currentTarget.dataset
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      api.wxphone({ encryptedData: e.detail.encryptedData, iv: e.detail.iv }, res => {
        if (res.data.code == 0) {
          let { phoneNumber } = res.data.entity.data
          data.phone = phoneNumber
          wx.setStorageSync('userInfo', data)
          that.setData({ data })
          wx.showToast({
            title: '授权成功',
            icon: 'none',
            duration: 1000
          })
          setTimeout(function () {
            wx.navigateTo({
              url: `../${url}/${url}`,
            })
          }, 1000)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      })
    } else {
      wx.navigateTo({
        url: `../${url}/${url}`,
      })
    }
  },
  to(e) {
    let { url } = e.currentTarget.dataset
    wx.navigateTo({
      url: `../${url}/${url}`,
    })
  },
  // 点击升级
  upgrade() {
    let { data } = that.data
    that.setData({ isupgrade: !0, isoldrel: !1 }, () => {
      api.transferold(data.oldRel, res => {
        that.setData({ isupgrade: !1 })
        if (res.data.code == 0) {
          wx.showToast({
            title: '升级成功',
            icon: 'none',
            duration: 1000,
            success() {
              that.getdata()
            }
          })
        } else {
          wx.showToast({
            title: '升级失败',
            icon: 'none',
            duration: 1000
          })
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let { isload } = that.data
    if (isload) {
      that.getdata()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    that.setData({ showgift: !1, shareKey: '' })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    that.setData({ showgift: !1, shareKey: '' })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})